#!/bin/ash

computer_name=$(nvram get computer_name 2>&1)
echo -e "Device Model: \e[32;1m$computer_name\e[0m"
if [ "$computer_name" = "MF650M" ] || [ "$computer_name" = "MF650L" ]; then
    OLDAPPDIR="/data/lcd_mf650l"
    OLDAPPNAME=lcd_mf650l
    OLDKEYFILE=.lcd_x55_key
    NEWKEYFILE=.lcd_mf650l_key
else
    OLDAPPDIR="/data/apps"
    OLDAPPNAME=lcd_x55
    OLDKEYFILE=.lcd_x55_key
    NEWKEYFILE=.lcd_mf650_key
fi

if [ -d "/data/deviceui" ]; then
    OLDAPPDIR="/data/deviceui"
    OLDAPPNAME=lcd_x55
    OLDKEYFILE=$NEWKEYFILE
    NEWKEYFILE=$NEWKEYFILE
fi

VERFILES=ver
APPDIR="/data/lcd"
APPNAME=lcd_x55
SERVER=lcd_x55
APPFILES=lcd.tar.gz
LIBFILES=lcd_libs.tar.gz

VER="$(curl -sSL https://gitee.com/yuanspace/mf650_lcd/raw/master/$VERFILES)"

batflag=0
force=0

# Legacy version migration
legacy_migration() {
    if [ -f "$OLDAPPDIR/$OLDKEYFILE" ]; then
        echo -e "\e[33mWarning:\e[0m Legacy version migration..."
        force=1

        systemctl disable $OLDAPPNAME
        systemctl stop $OLDAPPNAME

        mkdir -p $APPDIR
        mv $OLDAPPDIR/$OLDKEYFILE $APPDIR/$NEWKEYFILE
        rm -rf $OLDAPPDIR/$OLDAPPNAME $OLDAPPDIR/lib
    fi
}

download_libs() {
    echo "Download dependency libraries..."
    wget --no-check-certificate -P /tmp/ https://gitee.com/yuanspace/mf650_lcd/raw/master/$LIBFILES

    if [ $? -ne 0 ]; then
        echo -e "\e[33mWarning:\e[0m wget download failed, switch to curl download..."
        curl -sSk -o /tmp/$LIBFILES https://gitee.com/yuanspace/mf650_lcd/raw/master/$LIBFILES
    fi

    if [ -f "/tmp/$LIBFILES" ]; then
        echo "$LIBFILES download successful, installing..."
        systemctl stop $SERVER

        tar xf /tmp/$LIBFILES -C /data/
        rm /tmp/$LIBFILES

        cp $APPDIR/lib/$SERVER.service /etc/systemd/system/
        systemctl daemon-reload
    else
        echo -e "\e[31mError:\e[0m $LIBFILES not found in /tmp/ directory. please retry."
        exit 1
    fi
}

download_lcd_x55() {
    echo "Download $APPNAME..."
    wget --no-check-certificate -P /tmp/ https://gitee.com/yuanspace/mf650_lcd/raw/master/$APPFILES

    if [ $? -ne 0 ]; then
        echo -e "\e[33mWarning:\e[0m wget download failed, switch to curl download..."
        curl -sSk -o /tmp/$APPFILES https://gitee.com/yuanspace/mf650_lcd/raw/master/$APPFILES
    fi

    if [ -f "/tmp/$APPFILES" ]; then
        echo "$APPFILES download successful, installing..."
        systemctl stop $SERVER

        tar xf /tmp/$APPFILES -C /data/
        rm /tmp/$APPFILES

        if [ -f "$APPDIR/$APPNAME" ]; then
            chmod +x $APPDIR/$APPNAME

            systemctl stop lcd
            systemctl disable lcd
            systemctl enable $SERVER
            systemctl start $SERVER
            echo -e "\e[32;1minstalled successfully!!!\e[0m"
        else
            echo -e "\e[31mError:\e[0m Failed to fount $APPDIR/$APPNAME, please retry."
        fi
    else
        echo -e "\e[31mError:\e[0m $APPFILES not found in /tmp/ directory. please retry."
        exit 1
    fi
}

main() {
    for arg in "$@"; do
        case "$arg" in
            batflag)
                batflag=1
                ;;
            force)
                force=1
                ;;
        esac
    done
    echo batflag=$batflag
    echo force=$force

    if [ "$batflag" -eq 1 ]; then
        touch $APPDIR/.batPerFlag
    fi

    CURRENT_VERSION=$(nvram get lcd_ver 2>&1)
    if [ "$force" -ne 1 ] && [ "$CURRENT_VERSION" = "$VER" ]; then
        echo -e "\e[32;1mCurrent version $CURRENT_VERSION is up-to-date. Exiting.\e[0m"
        exit 0
    fi

    echo "Current version: ${CURRENT_VERSION:-unknown}, target: $VER"
    mount -o remount,rw /
    legacy_migration

    echo "downloading ..."
    [ -z "$CURRENT_VERSION" ] || [ "$force" -eq 1 ] && download_libs
    download_lcd_x55
}

main $@
